<?php

class ManageCategoryController extends AppController {

    var $name = 'ManageCategory';
    var $uses = array('Category', 'Categorybrand', 'Imagetitle', 'Field', 'Sizetitle');

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allowedActions = array('category_tree');
    }

    function index() {
        $this->layout = 'admin';
        $this->managecategory->recursive = 0;
        $this->set('category', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->User->create();
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The user has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        }
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The user has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->User->read(null, $id);
        }
    }

    function delete($id = null) {
        $this->data['cat_id'] = $id;
        $this->layout = false;
    }

    function category_tree() {
        $this->layout = false;
        $this->Category->recursive = 0;
        $this->data['category_tree'] = $this->Category->viewAll();
        foreach ($this->data['category_tree'] as $category):
            echo"<option value='" . $category['id'] . "'>";
            if ($category['depth'] > 1):
                for ($i = 0; $i < $category['depth']; $i++):
                    switch ($category['depth']) {
                        case 2: echo"&nbsp;";
                            break;
                        case 3: echo"&nbsp;&nbsp;";
                            break;
                        case 4: echo"&nbsp;&nbsp;";
                            break;
                        default: echo"";
                    }
                endfor;
            endif;
            echo $category['category_name'] . "</option>";
        endforeach;
        $this->autoRender = false;
    }

    function backstage_index() {
        $this->layout = 'admin';
        $this->Category->recursive = -1;
        $category_orders = $this->Category->find('list', array(
            'conditions' => array('Category.parent_id' => '0'),
            'order' => 'Category.category_order ASC',
            'fields' => 'Category.category_order'
                ));
        $categories = $this->Category->find('all', array(
            'conditions' => array('Category.parent_id' => '0'),
            'order' => 'Category.id ASC'));
        $i = 0;
        foreach ($categories as $category) {
            $categories[$i]['Category']['total_brands'] = $this->Categorybrand->total_brands($category['Category']['id']);
            $categories[$i]['Category']['total_styles'] = $this->Categorybrand->total_styles($category['Category']['id']);
            $categories[$i]['Category']['total_conditions'] = $this->Categorybrand->total_conditions($category['Category']['id']);
            $categories[$i]['Category']['total_sizes'] = $this->Sizetitle->total_sizes($category['Category']['id']);
            $categories[$i]['Category']['total_additional_fields'] = $this->Field->total_additional_fields($category['Category']['id']);
            $categories[$i]['Category']['total_imagetitles'] = $this->Imagetitle->total_imagetitles($category['Category']['id']);
            $categories[$i]['Category']['total_subcategories'] = $this->Category->total_subcategories($category['Category']['id']);
            $i++;
        }
        $this->set(compact('category_orders', 'categories'));
    }

    function backstage_categoryatglance() {
        $this->layout = 'admin';
        $this->set('categories', $this->Category->viewAll());
    }

    function backstage_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    function backstage_subcategories($id = null) {
        $this->layout = 'admin';
        $this->Category->recursive = -1;

        if (!$id) {
            $this->Session->setFlash(__('Invalid category', true));
            $this->redirect(array('action' => 'index'));
        }

        $this->data = $this->Category->read(null, $id);
        $breadcrumbs = "<li>" . $this->data['Category']['category_name'] . "</li>";
        $category_level = 1;
        $category_orders = $this->Category->find('list', array(
            'conditions' => array('Category.parent_id' => $id),
            'order' => 'Category.category_order ASC',
            'fields' => 'Category.category_order'));

        $categories = $this->Category->find('all', array(
            'conditions' => array('Category.parent_id' => $id),
            'order' => 'Category.category_name ASC'));
        $i = 0;
        foreach ($categories as $category) {
            $categories[$i]['Category']['total_brands'] = $this->Categorybrand->total_brands($category['Category']['id']);
            $categories[$i]['Category']['total_styles'] = $this->Categorybrand->total_styles($category['Category']['id']);
            $categories[$i]['Category']['total_conditions'] = $this->Categorybrand->total_conditions($category['Category']['id']);
            $categories[$i]['Category']['total_sizes'] = $this->Sizetitle->total_sizes($category['Category']['id']);
            $categories[$i]['Category']['total_additional_fields'] = $this->Field->total_additional_fields($category['Category']['id']);
            $categories[$i]['Category']['total_imagetitles'] = $this->Imagetitle->total_imagetitles($category['Category']['id']);
            $categories[$i]['Category']['total_subcategories'] = $this->Category->total_subcategories($category['Category']['id']);
            $i++;
        }
        if ($this->data['Category']['parent_id'] > 0) {
            $breadcrumb_1 = $this->Category->find("first", array('fields' => array('id', 'parent_id', 'category_name'),
                'conditions' => array('id' => $this->data['Category']['parent_id'])));
            $breadcrumbs = "<li><a href='" . Configure::read('base_url') . "backstage/manage_category/subcategories/" . $breadcrumb_1['Category']['id'] . "'>" . $breadcrumb_1['Category']['category_name'] . "</a></li><li>" . $breadcrumbs . "</li>";
            $category_level = 2;
            if ($breadcrumb_1['Category']['parent_id'] > 0) {
                $breadcrumb_2 = $this->Category->find("first", array('fields' => array('id', 'parent_id', 'category_name'),
                    'conditions' => array('id' => $breadcrumb_1['Category']['parent_id'])));
                $breadcrumbs = "<li><a href='" . Configure::read('base_url') . "backstage/manage_category/subcategories/" . $breadcrumb_2['Category']['id'] . "'>" . $breadcrumb_2['Category']['category_name'] . "</a></li><li>" . $breadcrumbs . "</li>";
                $category_level = 3;
                if ($breadcrumb_2['Category']['parent_id'] > 0) {
                    $breadcrumb_3 = $this->Category->find("first", array('fields' => array('id', 'parent_id', 'category_name'),
                        'conditions' => array('id' => $breadcrumb_2['Category']['parent_id'])));
                    $breadcrumbs = "<li><a href='" . Configure::read('base_url') . "backstage/manage_category/subcategories/" . $breadcrumb_3['Category']['id'] . "'>" . $breadcrumb_3['Category']['category_name'] . "</a></li><li>" . $breadcrumbs . "</li>";
                    $category_level = 4;
                }
            }
        }
        $this->data['category_level'] = $category_level;
        $this->data['breadcrumbs'] = $breadcrumbs;
        $this->set(compact('category_orders', 'categories'));
    }

    function backstage_add() {
        if (!empty($this->data)) {
            $this->Category->create();
            if ($this->Category->save($this->data)) {
                $this->Session->setFlash(__('The Category has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The Category could not be saved. Please, try again.', true));
            }
        }
    }

    function backstage_edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid user', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The user has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->User->read(null, $id);
        }
    }

    function backstage_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for category', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Category->delete($id, true)) {
            $this->Session->setFlash(__('Category has been deleted successfully.', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Category was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function backstage_confirmdelete() {
        $this->layout = false;
        if (!$this->data) {
            $this->Session->setFlash(__('Invalid confirmation for category delete', true));
            $this->redirect(array('action' => 'index'));
        }

        if ($this->data['confirm'] == "Yes") {
            $this->Category->deleteAll(array('Category.parent_id' => $this->data['category_id']));
            if ($this->Category->delete($this->data['category_id'], true)) {
                $this->Session->setFlash(__('Category has been deleted successfully.', true));
                $this->redirect(array('action' => 'success'));
            }
        } else {

            $this->Category->recursive = -1;
            $cat_name = $this->Category->findById($this->data['category_id']);
            $is_cat_name_exist = $this->Category->find("count", array("conditions" => array("Category.parent_id" => $this->data['assign_category'], "Category.category_name" => $cat_name['Category']['category_name'])));
            if ($is_cat_name_exist > 0) {
                $this->Session->setFlash(__('Sorry you cannot move it <b>(' . $cat_name . ')</b> Duplicate Category name.', true));
            } else {
                $this->Category->query("UPDATE authentic_categories SET parent_id='" . $this->data['assign_category'] . "' WHERE id='" . $this->data['category_id'] . "';");
                $this->Session->setFlash(__('Category has been reassigned and deleted successfully.', true));
            }
            $this->redirect(array('action' => 'success'));
        }
    }

    function backstage_success() {
        $this->layout = false;
    }

    function backstage_updateorder($id1= null, $id2= null, $id3= null) {
        if (!empty($id3)) {
            $this->data['category_order'] = $this->Category->find('list', array(
                'conditions' => array('Category.parent_id' => $id3),
                'order' => 'Category.category_order ASC',
                'fields' => 'Category.category_order')
            );
        } else {
            $this->data['category_order'] = $this->Category->find('list', array(
                'conditions' => array('Category.parent_id' => '0'),
                'order' => 'Category.category_order ASC',
                'fields' => 'Category.category_order'));
        }
        $flag = '';
        $i = 1;
        $items = array();
        foreach ($this->data['category_order'] as $key => $val):
            if ($id1 == $key) {
                if (!$id2) {
                    $flag = $i - 1;
                } else {
                    $flag = $id2;
                }
            } else {
                $items[] = $key;
            }
            $i++;
        endforeach;
        array_splice($items, ($flag - 1), 0, $id1);
        $i = 1;
        foreach ($items as $val) {
            $i_pad = str_pad($i, 4, "0", "0");
            $this->data['Category']['id'] = $val;
            $this->data['Category']['category_order'] = $i_pad;
            $this->Category->save($this->data);
            $i++;
        }

        $this->Session->setFlash(__('Category order has been updated', true));
        if (!empty($id3))
            $this->redirect(array('action' => 'subcategories', $id3));
        else
            $this->redirect(array('action' => 'index'));
    }

}

?>